package Modelo;

/*
 * Proyectos.java
 *
 * Created on 13 de febrero de 2007, 07:45 PM
 */
/**
 *
 * @author  lebela
 */
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import Modelo.*;
import com.puj.bdutils.ConnectorBD;

public class EjecutaConsulta {

    public static Vector Ejecutar(String p_consulta) throws ServletException, java.io.IOException, Exception {
        String control = new String();
        Statement s = null;
        Connection conn = null;
        ResultSet rs = null;
        Vector v = new Vector();
        Fila f = null;
        try {


            conn = ConnectorBD.getConnection(Path.ruta);
            System.out.println(p_consulta + " " + conn);
            s = conn.createStatement();

            rs = s.executeQuery(p_consulta);
            ResultSetMetaData rsm = rs.getMetaData();
            while (rs.next()) {
                f = new Fila();
                for (int i = 1; i <= rsm.getColumnCount(); i++) {
                    f.adicionar(rsm.getColumnLabel(i).toUpperCase(), rs.getString(i));
                }
                v.addElement(f);
            }
            s.close();
            rs.close();
            conn.close();
        } catch (Exception e) {
            s.close();
            rs.close();
            conn.close();
            System.out.println("Error : " + e);
        }
        return v;
    }

    public static Fila Ejecutar2(String p_consulta) throws ServletException, java.io.IOException, Exception {
        String control = new String();
        Statement s = null;
        Connection conn = null;
        ResultSet rs = null;
        Fila f = new Fila();
        try {

            conn = ConnectorBD.getConnection(Path.ruta);
            System.out.println(p_consulta + " " + conn);
            s = conn.createStatement();

            rs = s.executeQuery(p_consulta);
            ResultSetMetaData rsm = rs.getMetaData();
            if (rs.next()) {
                f = new Fila();
                for (int i = 1; i <= rsm.getColumnCount(); i++) {
                    f.adicionar(rsm.getColumnLabel(i).toUpperCase(), rs.getString(i));
                }
            }
            s.close();
            rs.close();
            conn.close();
        } catch (Exception e) {
            s.close();
            rs.close();
            conn.close();
            System.out.println("Error : " + e);
        }
        return f;
    }

    /** Método que permite enviar un script para insertar datos
     * @param string con el comando a insertar
     */
    public static boolean Insertar(String p_consulta) throws ServletException, java.io.IOException, Exception {
        String control = new String();
        Statement s = null;
        Connection conn = null;
        boolean salida = false;

        try {
            conn = ConnectorBD.getConnection(Path.ruta);
            System.out.println(p_consulta + " " + conn);
            s = conn.createStatement();
            s.execute(p_consulta);
            salida = true;
            s.close();
            conn.close();
        } catch (Exception e) {
            s.close();
            salida = false;
            conn.close();
            System.out.println("Error : " + e);
        }
        return salida;
    }

    /** Método que permite enviar un script para actualizar datos
     * @param string con el comando a actualizar
     */
    public static boolean Actualizar(String p_consulta) throws ServletException, java.io.IOException, Exception {
        String control = new String();
        Statement s = null;
        Connection conn = null;
        boolean salida = false;

        try {
            conn = ConnectorBD.getConnection(Path.ruta);
            System.out.println(p_consulta + " " + conn);
            s = conn.createStatement();
            s.execute(p_consulta);
            salida = true;
            s.close();
            conn.close();
        } catch (Exception e) {
            s.close();
            salida = false;
            conn.close();
            System.out.println("Error : " + e);
        }
        return salida;
    }

    public static void main(String[] args) {
        String consulta = "Select * from sg_datos_parametros";
        // EjecutaConsulta ec = new EjecutaConsulta();
        try {
            EjecutaConsulta.Ejecutar(consulta);
        } catch (Exception e) {
            System.out.println("errro " + e.getMessage());
        }
    }
}
